package 链表;

import common.ListNode;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author tmh
 * @date 2024/8/21 20:32
 * @description
 */
public class T148排序链表 {
    public ListNode sortList(ListNode head) {
        //把链表里面的元素存在集合里面，然后排序后在还原回去
        ListNode p = head;
        List<Integer> list = new ArrayList();
        while (p != null) {
            list.add(p.val);
            p = p.next;
        }
        list.sort(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return (int) o1 - (int) o2;
            }
        });
        p = head;
        for (int i = 0; i < list.size(); i++) {
            p.val = list.get(i);
            p = p.next;
        }
        return head;
    }
}
